<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.23.
   家居公司装修日记模型($$)*/

!defined('Q_PATH') && exit;

class JiajudiaryModel extends CommonModel{

	static public function init__(){
		return array(
			'table_name'=>'jiajudiary',
			'check'=>array(
				'jiajudiary_sort'=>array(
					array('number',Q::L('序号只能是数字','__COMMON_LANG__@Common')),
				),
				'jiajudiary_title'=>array(
					array('require',Q::L('日记标题不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',100,Q::L('日记标题不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajudiary_link'=>array(
					array('require',Q::L('日记链接不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',200,Q::L('日记链接不能超过200个字符','__APPJIAJU_COMMON_LANG__@Model')),
					array('url',Q::L('日记链接必须为正确的URL格式','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajudiary_user'=>array(
					array('require',Q::L('日记帖子作者不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',50,Q::L('日记帖子作者不能超过50个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
			),
		);
	}

	static function F(){
		$arrArgs=func_get_args();
		return ModelMeta::instance(__CLASS__)->findByArgs($arrArgs);
	}

	static function M(){
		return ModelMeta::instance(__CLASS__);
	}

	protected function beforeSave_(){
		$this->jiajudiary_title=C::text($this->jiajudiary_title);
		$this->jiajudiary_user=C::text($this->jiajudiary_user);
		$this->jiajudiary_link=C::strip($this->jiajudiary_link);
		$this->jiajudiary_username=C::text($this->jiajudiary_username);
		$this->jiajudiary_editusername=C::text($this->jiajudiary_editusername);
		
		if($this->jiajudiary_sort<0){
			$this->jiajudiary_sort=0;
		}
		if($this->jiajudiary_sort>999){
			$this->jiajudiary_sort=999;
		}
	}

	protected function beforeCreate_(){
		if(Q::G('jiajucompany_id')){
			$arrCompany=Model::F_('jiajucompany','jiajucompany_id=?',intval(Q::G('jiajucompany_id')))->setColumns('jiajucompany_id,site_id,province_id,city_id')->getOne();
			
			// 商家&城市
			$this->jiajucompany_id=$arrCompany['jiajucompany_id'];
			$this->site_id=$arrCompany['site_id'];
			$this->province_id=$arrCompany['province_id'];
			$this->city_id=$arrCompany['city_id'];
		}

		// 添加人
		$this->user_id=$GLOBALS['___login___']['user_id'];
		$this->jiajudiary_username=$GLOBALS['___login___']['user_name'];
		$this->jiajudiary_editusername=$GLOBALS['___login___']['user_name'];
		$this->jiajudiary_edituserid=$GLOBALS['___login___']['user_id'];
	}
	
	protected function beforeUpdate_(){
		if(Q::G('jiajucompany_id')){
			$arrCompany=Model::F_('jiajucompany','jiajucompany_id=?',intval(Q::G('jiajucompany_id')))->setColumns('jiajucompany_id,site_id,province_id,city_id')->getOne();
			
			// 商家&城市
			$this->jiajucompany_id=$arrCompany['jiajucompany_id'];
			$this->site_id=$arrCompany['site_id'];
			$this->province_id=$arrCompany['province_id'];
			$this->city_id=$arrCompany['city_id'];
		}
		
		// 更新人
		$this->jiajudiary_editusername=$GLOBALS['___login___']['user_name'];
		$this->jiajudiary_edituserid=$GLOBALS['___login___']['user_id'];
	}

}
